const app = getApp();
var {
  Api
} = require('../../api/api')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    flage:false,//是否公开
    textarea:'',//笔记内容
    id:'',//题目id
    images:[],//上传的图片
    images2:[],//上传的图没有前缀
    bjid:'',//笔记id
    domain: Api.domain,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    if(options.bjid){
      this.setData({
        bjid:options.bjid,
      })
      this.init()
    }
    this.setData({
      id:options.id
    })
  },
  chooseimg(){//上传图片
    var that = this
    wx.chooseImage({
      count: 10,
      sizeType: ['original', 'compressed'],
      sourceType: ['album', 'camera'],
      success: (res) => {
        // tempFilePath可以作为img标签的src属性显示图片
        const tempFilePaths = res.tempFilePaths
        console.log(res)
        wx.showLoading({
          title: '加载中',
        })
       for(let i = 0;i<tempFilePaths.length;i++){
        wx.uploadFile({
          url: wx.$get.upload_img,
          filePath: tempFilePaths[i],
          name: "file",
          formData: {
            key:  wx.$cache.get('key'),
            file_type:3
          },
          success: (result) => {
            wx.hideLoading()
            console.log(result)
            const item = JSON.parse(result.data)
            console.log(item)
            that.setData({
              images:that.data.images.concat(item.datas.url),
              images2:that.data.images2.concat(item.datas.file_url)
            })
          },
        });
       }
      }
    })
  },
  init(){//笔记详情
    var that = this
    var params = {
      note_id:this.data.bjid,
      member_id:wx.$cache.get('member_id')
    }
    wx.$http.get(wx.$get.note_info,params).then(res=>{
      console.log(res,'笔记详情')
      this.setData({
        images:res.imgs?res.imgs:[],
        textarea:res.content
       })
    })
  },
  switch(){
    this.setData({
     flage:!this.data.flage 
    })
  },
  bindinput(e){//笔记内容
    this.setData({
      textarea:e.detail.value
    })
  },
  addNote(){//提交创建笔记
    var that = this
    let imgs = this.data.images2.toString()
    let imgs2 = this.data.images.toString()
    if(this.data.bjid){
      var params= {
        note_id:this.data.bjid,
        content:this.data.textarea,
        is_public:this.data.flage?2:1,
        imgs:imgs2
      }
      wx.$http.get(wx.$get.note_edit,params).then(res=>{
        console.log(res,'修改笔记')
        var pageArr = getCurrentPages();
        var thisPage = pageArr[pageArr.length - 2];
        if(thisPage.route=="pages/myNotes/myNotes"){
          thisPage.mybi()
          thisPage.mybiid()
        }
        wx.navigateBack({
          delta: 1,
        })
      })
    }else{
      var params = {
        subject_id:this.data.id,
        content:this.data.textarea,
        is_public:this.data.flage?2:1,
        imgs,
      }
      wx.$http.get(wx.$get.note_add,params).then(res=>{
        console.log('创建笔记',res)
        var pageArr = getCurrentPages();
        var thisPage = pageArr[pageArr.length - 2];
        var thisPage2 = pageArr[pageArr.length - 3];
        console.log('router',thisPage.route)
        if(thisPage.route=="pages/brushTopic/brushTopic"||thisPage2.route=="pages/brushTopic/brushTopic"){
          thisPage.tidetail2()
        }
        wx.navigateBack({
          delta: 1,
        })
      })
    }
  },
  closefu(e){//删除照片
    const {index} = e.currentTarget.dataset
    let {images} = this.data
    let {images2} = this.data
    images.splice(index,1);
    images2.splice(index,1)
    this.setData({
      images,
      images2
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    return{
      title:wx.$cache.get('fx').title,
      imageUrl:wx.$cache.get('fx').img,
      path:`/pages/index/index?pid=${wx.$cache.get('member_id')}`
    }
  }
})